package com.spark.market.dal.repository.impl;

import com.spark.market.common.exception.BusinessException;
import com.spark.market.common.exception.EmBusinessError;
import com.spark.market.common.utils.AssertUtils;
import com.spark.market.dal.dao.UserInfoDoMapper;
import com.spark.market.dal.entity.UserInfoDo;
import com.spark.market.dal.repository.UserRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class UserRepositoryImpl implements UserRepository {
    @Resource
    private UserInfoDoMapper userInfoDoMapper;
    @Override
    public UserInfoDo selectUserById(Integer userId) {
        AssertUtils.isNotNull(userId, "userId is null");
        userInfoDoMapper.selectByPrimaryKey(userId);
        return userInfoDoMapper.selectByPrimaryKey(userId);
    }

    @Override
    public void insertUser(UserInfoDo userInfoDo) {
        if (StringUtils.isEmpty(userInfoDo.getName())
        || StringUtils.isEmpty(userInfoDo.getRegisterMode())
        || StringUtils.isEmpty(userInfoDo.getTelphone())
        || userInfoDo.getAge()==null
        || userInfoDo.getGender() == null){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        //在mapper中加入，插入数据后将id赋值keyProperty="id" useGeneratedKeys="true"
        userInfoDoMapper.insertSelective(userInfoDo);
    }

    @Override
    public UserInfoDo selectUserByTelePhone(String telephone) {
        AssertUtils.isNotNull(telephone, "telephone is null");
        return userInfoDoMapper.selectByTelphone(telephone);
    }
}
